package com.huicoo.forestmanager.base;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;

import com.huicoo.forestmanager.util.DisplayUtil;
import com.huicoo.forestmanager.util.LogUtils;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import butterknife.ButterKnife;

public abstract class BaseActivity extends AppCompatActivity {
    public BasePresenter presenter = null;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LogUtils.v("---生命周期---：" + this.getClass().getName() + " create");
        // 设置activity无标题栏
        if (getSupportActionBar() != null) {
            getSupportActionBar().hide();
        }
        // 设置activity横屏
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

        setContentView(getLayoutId());
        // 屏幕适配
        DisplayUtil.setCustomDensity(this, BaseApplication.getMyApplication());

        initBind();

        initView();
    }

    @Override
    protected void onDestroy() {
        LogUtils.v("---生命周期---：" + this.getClass().getName() + " destroy");
        super.onDestroy();
        if (presenter != null) {
            presenter.destroy();
            presenter = null;
        }
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    protected abstract int getLayoutId();

    protected abstract void initView();

    private void initBind() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}
